from datetime import datetime

from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtWidgets import QLabel, QLCDNumber, QVBoxLayout, QWidget


class ClockPage(QWidget):
    """时钟显示页面"""

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)

        # 创建LCD数字时钟
        self.lcd = QLCDNumber()
        self.lcd.setDigitCount(19)  # YYYY-MM-DD HH:MM:SS 共19字符
        self.lcd.setSegmentStyle(QLCDNumber.Filled)
        self.lcd.setStyleSheet("""
            QLCDNumber {
                background: #000;
                color: #0f0;  /* 绿色数字 */
                border-radius: 10px;
            }
        """)

        # 标题标签
        title = QLabel("系统时间")
        title.setStyleSheet("font-size: 24px; font-weight: bold; color: #333;")
        title.setAlignment(Qt.AlignCenter)

        # 布局设置
        layout.addWidget(title)
        layout.addWidget(self.lcd, 1)
        layout.setAlignment(self.lcd, Qt.AlignCenter)

        # 初始化时间
        self.update_time()

        # 定时器刷新
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_time)
        self.timer.start(1000)  # 每秒刷新

    def update_time(self):
        """更新时间显示"""
        current_date = datetime.today().strftime("%Y-%m-%d")
        current_time = datetime.today().strftime("%H:%M:%S")
        self.lcd.display(f"{current_date} {current_time}")
